package com.gl.emms.whale.ui;

import java.util.Timer;
import java.util.TimerTask;

import android.content.Intent;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;

import com.gl.emms.client.android.EMMSPushManager;
import com.gl.emms.nio.mutual.ReplyBody;
import com.gl.emms.whale.R;
import com.gl.emms.whale.app.Constant;
import com.gl.emms.whale.app.MyApplication;
import com.gl.emms.whale.app.WhaleMonitorActivity;
import com.gl.emms.whale.util.DialogFactory;

/**报修界面类
 * @author ganlinux
 *
 */
public class ReportRepairActivity extends WhaleMonitorActivity implements
OnClickListener{
	Timer timer;//定时器
	
	private Button repairButton;//文字报修按钮
	private Button repairCallButton;//电话报修按钮
	private EditText reportContent;//报修内容
	private RadioButton reportTypeRepair,reportTypeBug;//报修类型单选按钮

	private String reportType;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);// 去掉标题栏
		setContentView(R.layout.activity_reportrepair);

		initViews();
	}
	/**
	 * 界面初始化
	 * @time 2014年11月13日上午10:59:03
	 * @return void
	 * @user ganlinux
	 */
	public void initViews() {
		Button titleBackBtn = (Button)findViewById(R.id.TitleBackBtn); 
		titleBackBtn.setOnClickListener(this);

		reportContent = (EditText) findViewById(R.id.txtContent);
		repairButton = (Button)findViewById(R.id.btn_report); 
		repairButton.setOnClickListener(this);
		repairCallButton = (Button)findViewById(R.id.btn_report_by_phone); 
		repairCallButton.setOnClickListener(this);

		reportTypeRepair = (RadioButton) findViewById(R.id.reporttyperepair);
		reportTypeBug = (RadioButton) findViewById(R.id.reporttypebug);
	}
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btn_report:
			if (reportContent.getText().toString().equals("")) {
				showToask(getString(R.string.no_content_tip));
				return;
			}
			if (reportTypeRepair.isChecked()) {
				reportType = Constant.REPORT_TYPE_TEXT;
			}else {
				reportType = Constant.REPORT_TYPE_CALL;
			}
			showProgressDialog("提示", "正在提交，请稍后......");
			
			EMMSPushManager.requestReportRepair(this,MyApplication.getUserData().getIdIP(),MyApplication.getUserData().getMainroom(),reportType,reportContent.getText().toString());
			//startTimer(30);
			break;
		case R.id.btn_report_by_phone:
			//用intent启动拨打电话  
			Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse(Constant.REPORT_PHONENUM));  
			startActivity(intent);  
			break;
		case R.id.TitleBackBtn:
			//返回
			//timerCancle();
			new KeyEvent(KeyEvent.ACTION_DOWN,KeyEvent.KEYCODE_BACK);
			finish();
			break;
		default:
			break;
		}
	}
	@Override
	public void onReplyReceived(final ReplyBody reply) {
		super.onReplyReceived(reply);
		if (reply.getType().equals(Constant.TYPE_REPORT_RESPONSE)) {
			hideProgressDialog();
			//timerCancle();
			if (reply.get("reportstate").equals("000001")) {
				DialogFactory.ToastDialog(ReportRepairActivity.this,"设备报修",
						"成功");
			}else {
				DialogFactory.ToastDialog(ReportRepairActivity.this,"设备报修",
						"失败");
			}
			
		}

	}
	public void startTimer(int secends){

		TimerTask task = new TimerTask(){   
			public void run() {
				timerCancle();
				timerStop();
			}   
		};
		timer = new Timer(true);  
		timer.schedule(task,secends*1000);
	}
	public void timerCancle(){
		timer.cancel();
	}
	public void timerStop(){
		Message message = new Message();      
		message.what = 0x02;      
		handler.sendEmptyMessage(message.what);
	}
	Handler handler = new Handler(){  
		public void handleMessage(Message msg) {  
			switch (msg.what) {      
			case 0x02: 
				hideProgressDialog();
				DialogFactory.ToastDialog(ReportRepairActivity.this,"设备报修",
						"失败，通讯失败，请重试");
				break;      
			}      
			super.handleMessage(msg);  
		}    
	}; 
}
